﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyChannel.DownloadComponent
{
    public class LocalViewModel : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify Silverlight that a property has changed.
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion



        // LINQ to SQL data context for the local database.
        private MyDataContext myDB;

        // Class constructor, create the data context object.
        public LocalViewModel(string toDoDBConnectionString)
        {
            myDB = new MyDataContext(toDoDBConnectionString);
        }

        // A list of all categories, used by the add task page.
        private List<LocalPlaylist> _playlistList;
        public List<LocalPlaylist> PlaylistList
        {
            get { return _playlistList; }
            set
            {
                _playlistList = value;
                NotifyPropertyChanged("CategoriesList");
            }
        }

        private List<LocalVideo> _videoList;
        public List<LocalVideo> VideoList
        {
            get { return _videoList; }
            set
            {
                _videoList = value;
                NotifyPropertyChanged("VideoList");
            }
        }

        // Write changes in the data context to the database.
        public void SaveChangesToDB()
        {
            myDB.SubmitChanges();
        }

        // Query database and load the collections and list used by the pivot pages.
        public void LoadCollectionsFromDatabase()
        {

            VideoList = myDB.Videos.ToList();

            // Load a list of all categories.
            PlaylistList = myDB.Playlists.ToList();
        }

        public void AddPlaylist(LocalPlaylist playlist)
        {
            PlaylistList.Add(playlist);

            // Add a to-do item to the data context.
            myDB.Playlists.InsertOnSubmit(playlist);

            // Save changes to the database.
            myDB.SubmitChanges();
        }

        public void DeletePlaylist(LocalPlaylist playlist)
        {
            PlaylistList.Remove(playlist);
            while (playlist.Videos.Count > 0)
            {
                DeleteVideo(playlist.Videos[0]);
            }
            myDB.Playlists.DeleteOnSubmit(playlist);
            myDB.SubmitChanges();
        }

        // Add a to-do item to the database and collections.
        public void AddVideo(LocalVideo video)
        {
            VideoList.Add(video);

            // Add a to-do item to the data context.
            myDB.Videos.InsertOnSubmit(video);

            // Save changes to the database.
            myDB.SubmitChanges();
        }

        // Remove a to-do task item from the database and collections.
        public void DeleteVideo(LocalVideo video)
        {
            int count = 0;
            foreach (LocalVideo s in myDB.Videos)
            {
                if (s.Key == video.Key)
                    count++;
            }
            if (video.State != "Completed")
            {
                int index = 0;
                while (index < ModelLocator.HomeStatic.Downloads.Count)
                {
                    if (ModelLocator.HomeStatic.Downloads[index].Video == video)
                    {
                        if (video.State == "Downloading")
                        {
                            ModelLocator.HomeStatic.Downloads[index].StopDownload();
                        }
                        ModelLocator.HomeStatic.Downloads.RemoveAt(index);
                        break;
                    }
                    index++;
                }
            }
            myDB.Videos.DeleteOnSubmit(video);
            myDB.SubmitChanges();
            if (count == 1)
            {
                try
                {
                    IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
                    if (file.FileExists(App.DIRECTORY_SAVE_DOWNLOAD + video.Key))
                    {
                        file.DeleteFile(App.DIRECTORY_SAVE_DOWNLOAD + video.Key);
                    }
                }
                catch { }
            }
            this.VideoList.Remove(video);
        }
    }
}
